package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.plus.web.page.PageDto;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.SysLoginLog;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.params.SysLoginLogParams;
import com.ruoyi.system.service.ISysLoginLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/login_log")
public class SysLoginLogController extends BasePlusController {
    @Autowired
    private ISysLoginLogService loginLogService;

    @PreAuthorize("@ss.hasPermi('system:login_log:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysLoginLogParams loginLog) {
        PageDto pageDto = startPage();
        Page<SysLoginLog> page = new Page(pageDto.getCurrentPage(), pageDto.getPageSize());

        Page<SysLoginLog> list = loginLogService.selectLoginLogList(page,loginLog);
        return getDataTable(list);
    }

    @Log(title = "登陆日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:login_log:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginLogParams loginLog) throws IOException {
        Page<SysLoginLog> page = new Page<>(1, -1);
        List<SysLoginLog> list = loginLogService.selectLoginLogList(page,loginLog).getRecords();
        ExcelUtil<SysLoginLog> util = new ExcelUtil<SysLoginLog>(SysLoginLog.class);
        util.exportExcel(response, list, "登陆日志");
    }

    @PreAuthorize("@ss.hasPermi('system:login_log:remove')")
    @Log(title = "登陆日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable Long[] ids) {
        return R.ok(loginLogService.deleteLoginLogByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('system:login_log:remove')")
    @Log(title = "登陆日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public AjaxResult clean() {
        loginLogService.cleanLoginLog();
        return AjaxResult.success();
    }
}
